﻿using System.Collections.Generic;
using Orchard.Environment.Extensions.Models;
using Orchard.Security.Permissions;

namespace Dyn.News {
    public class Permissions : IPermissionProvider {
        public static readonly Permission ManageCategory = new Permission { Description = "Manage Category", Name = "ManageCategory" };

        public static readonly Permission PublishArticle = new Permission { Description = "Publish or unpublish Article for others", Name = "PublishArticle", ImpliedBy = new[] { ManageCategory } };
        public static readonly Permission PublishOwnArticle = new Permission { Description = "Publish or unpublish own article post", Name = "PublishOwnArticle", ImpliedBy = new[] { PublishArticle } };
        public static readonly Permission EditArticle = new Permission { Description = "Edit any article", Name = "EditArticle", ImpliedBy = new[] { PublishArticle } };
        public static readonly Permission EditOwnArticle = new Permission { Description = "Edit own article", Name = "EditOwnArticle", ImpliedBy = new[] { EditArticle, PublishOwnArticle } };
        public static readonly Permission DeleteArticle = new Permission { Description = "Delete article for others", Name = "DeleteArticle", ImpliedBy = new[] { ManageCategory } };
        public static readonly Permission DeleteOwnArticle = new Permission { Description = "Delete own article", Name = "DeleteOwnArticle", ImpliedBy = new[] { DeleteArticle } };

        public static readonly Permission MetaListCategory = new Permission { ImpliedBy = new[] { EditArticle, PublishArticle, DeleteArticle } };
        public static readonly Permission MetaListOwnCategory = new Permission { ImpliedBy = new[] { EditOwnArticle, PublishOwnArticle, DeleteOwnArticle } };

        public virtual Feature Feature { get; set; }

        public IEnumerable<Permission> GetPermissions() {
            return new[] {
                ManageCategory,
                EditOwnArticle,
                EditArticle,
                PublishOwnArticle,
                PublishArticle,
                DeleteOwnArticle,
                DeleteArticle,
            };
        }

        public IEnumerable<PermissionStereotype> GetDefaultStereotypes() {
            return new[] {
                new PermissionStereotype {
                    Name = "Administrator",
                    Permissions = new[] {ManageCategory}
                },
                new PermissionStereotype {
                    Name = "Editor",
                    Permissions = new[] {PublishArticle,EditArticle,DeleteArticle}
                },
                new PermissionStereotype {
                    Name = "Moderator",
                },
                new PermissionStereotype {
                    Name = "Author",
                    Permissions = new[] {PublishOwnArticle,EditOwnArticle,DeleteOwnArticle}
                },
                new PermissionStereotype {
                    Name = "Contributor",
                    Permissions = new[] {EditOwnArticle}
                },
            };
        }

    }
}


